﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FanaticGene.CommandLineSerializer
{
    [System.AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = true)]
    public sealed class ArgumentAttribute : Attribute
    {
        public string Key { get; set; }
        public string DefaultValue { get; set; }
        public bool IsAbsolutePath { get; set; }
        public bool HasValue { get; set; }

        public ArgumentAttribute(string key)
        {
            if(key == null)
                throw new ArgumentNullException(nameof(key));
            if (key.Length == 0)
                throw new ArgumentNullException("键字符串不能为空。");
            if (IsValidString(key) == false)
                throw new ArgumentNullException("键不可以全为空格。");
            Key = key;
            HasValue = false;
        }

        public ArgumentAttribute(string key, string value) : this(key, value, false)
        {

        }

        /// <summary>
        /// 命令行参数特性。
        /// </summary>
        /// <param name="key">命令行键名。</param>
        /// <param name="value">命令行值内容。</param>
        /// <param name="isAbsolutePath">指定该值是否为地址，标记后，会检查本机是否存在该路径。</param>
        /// <exception cref="ArgumentNullException"></exception>
        public ArgumentAttribute(string key, string value, bool isAbsolutePath)
        {
            if (key.Length == 0)
                throw new ArgumentNullException("键字符串不能为空。");
            if (IsValidString(key) == false)
                throw new ArgumentNullException("键不可以全为空格。");
            Key = key;
            DefaultValue = value;
            IsAbsolutePath = isAbsolutePath;
            HasValue = true;
        }

        private bool IsValidString(string value)
        {
            foreach (var item in value)
            {
                if (item == ' ') continue;
                else return true;
            }
            return false;
        }


    }
}
